from django.http import *
from django.shortcuts import *
from django import forms
from ytbase.helper import *
from ytbase.model.post import YTPost
from ytbase.model.comment import YTComment
from ytbase.model.user import YTUser
  
class CommentForm(forms.Form):
    content = forms.CharField(required=False, widget=forms.Textarea)

@authorize(YTComment, OPERATION_ADD)
def add(request, id, filler=None):
    if request.method == 'POST':
        form = CommentForm(request.POST)
        if form.is_valid():
            post = get_by_str_id(YTPost, id)
            comment = YTComment(ref=post)
            comment.author = YTUser.get_current_user()
            comment.content = form.cleaned_data['content']
            
            if filler is not None:
                comment = filler(comment)
            
            comment.put()
        return HttpResponseRedirect(request.META['HTTP_REFERER'])
    return HttpResponseNotFound()

@check_id(YTComment)
@authorize(YTComment, OPERATION_DELETE)
def delete(request, id):
    if request.method == 'POST':
        model = get_by_str_id(YTComment, id)
        model.delete()
        return HttpResponse('Deleted!')
    return HttpResponseNotFound()
